///////////////////////////////////////////////////////////////////////////////////////////////////
// Illusion Ray Development Kit (IDK)           Copyright (c) 2010 ( dimitriy.martynenko@gmail.com )
///////////////////////////////////////////////////////////////////////////////////////////////////
// Created : 2010-05-22
// Updated : ...
// Licence : NEW BSD
// Info	   : IDK Engine
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once

namespace Core
{
	/*-------------------------------------------------------------------------
	| TRenderTexture:
	| ------------
	| TODO: need add information
	---------------------------------------------------------------------------*/
	class TRenderTexture : public TObject, virtual public IDK::IRenderTexture
	{
		IDKClass;
	private:
		uint mHeight;
		uint mWidth;
		IDK::Pixel::Type::Enum mPixelType;
		IDK::GPURenderTexture mTexture;
	private:
		void BindGpuResource(IDK::IRenderState& rndState)
		{
			if(mTexture == NullPtr || mTexture->IsInvalid())
			{
				mTexture = rndState.CreateRenderTexture(
					GetWidth(),
					GetHeight(),
					GetPixelType(),
					GetFilter());
			}
		}
	public:
		TRenderTexture(uint width, uint height, IDK::Pixel::Type::Enum pixelType) 
			: mHeight(height), mWidth(width), mPixelType(pixelType)
		{
		}

		virtual uint GetWidth(void)
		{
			return mWidth;
		}

		virtual uint GetHeight(void)
		{
			return mHeight;
		}

		virtual void SetPixelType(IDK::Pixel::Type::Enum fmt)
		{
			mPixelType = fmt;
		}

		virtual IDK::Pixel::Type::Enum GetPixelType(void)
		{
			return mPixelType;
		}

		virtual uint GetMipCount(void)
		{
			return 1; //mip was not support, only use the first level
		}

		virtual IDK::Pixel::Filter::Enum GetFilter(void) const
		{
			return IDK::Pixel::Filter::Point;
		}

		virtual void BindTexture(IDK::IRenderState& rndState, uint index)
		{
			BindGpuResource(rndState);
			rndState.SetTexture(mTexture.As<IDK::IGPUTexture>(), index);
		}

		virtual void Attach(IDK::IRenderState& rndState, uint index)
		{
			BindGpuResource(rndState);
			rndState.SetRenderTexture(mTexture, index);
		}
	};

	typedef ref<TRenderTexture> RenderTexture;

	/*-------------------------------------------------------------------------
	| TRenderTarget:
	| ------------
	| TODO: need add information
	---------------------------------------------------------------------------*/
	class TRenderTarget : public TArray<IDK::RenderTexture>, virtual public IDK::IRenderTarget
	{
		typedef TArray<IDK::RenderTexture> Base;
		IDKClass;
	private:
		String mName;
		ref<IDK::IRenderTexture> mDepthTexture;
		IDK::GPURenderTarget mTarget;
	private:
		void BindGpuResource(IDK::IRenderState& rndState);
	public:
		TRenderTarget(const String& name);
	public:
		virtual RenderTextures	GetRenderTextures(void);
		virtual ref<IDK::IRenderTexture> GetDepthTexture(void);
		virtual void			AttachTexture(ref<IDK::IRenderTexture> rndTex);
		virtual void			AttachDepth(ref<IDK::IRenderTexture> depthTex);
		virtual bool			Render(IDK::IRenderState& rndState);
		virtual void			Rendered(IDK::IRenderState& rndState);
	};

	typedef ref<TRenderTarget> RenderTarget;
}
